package com.indexingsystem.db.mongoDB.impl;

import java.util.Arrays;
import java.util.List;

import org.bson.Document;
import org.bson.conversions.Bson;

import com.indexingsystem.boss.entity.AuditLogs;
import com.indexingsystem.boss.entity.DhResourceauditlogs;
import com.indexingsystem.boss.entity.DhTodayStorageInfos;
import com.indexingsystem.boss.entity.Resources;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.boss.utils.JsonUtils;
import com.indexingsystem.db.mongoDB.MongoManager;
import com.indexingsystem.db.mongoDB.comon.MongoDBUtils;
import com.mongodb.client.model.Filters;

/**
 * 资源公共mongo处理类.
 * 
 * @author Administrator
 * 
 */
public class ResourceCommonMongo {

	/**
	 * 批量审核同步修改审核状态.
	 * 
	 * @param resIdList
	 * @param status
	 * @param connName
	 */
	public static void updateResourceStatus(List<String> resIdList, int status, String connName, String institutionId) throws Exception {
		MongoManager manager = new MongoManager();
		for (String resId : resIdList) {
			Bson filter = Filters.and( Filters.in( "resId", resId ), Filters.eq( "institutionId", institutionId ) );
			Bson bson = Filters.and( Filters.in( "resId", resId ), Filters.eq( "institutionId", institutionId ) );
			LogCvt.info( connName + "获取资源查询条件:" + Arrays.asList( bson ).toString() );
			List<Document> documentsList = manager.queryAggregateList( bson, null, connName );
			if (null != documentsList && documentsList.size() > 0) {
				for (Document document : documentsList) {
					Resources resources = new Resources();
					resources = (Resources) MongoDBUtils.setFieldValue( resources, Document.parse( JsonUtils.toJson( document.get( "resources" ) ) ) );
					if (null != resources) {
						resources.setStatus( status );
						Document resourcesDoc = MongoDBUtils.transform2Doc( resources );
						document.put( "resources", resourcesDoc );
					}

					AuditLogs auditLogs = new AuditLogs();
					auditLogs = (AuditLogs) MongoDBUtils.setFieldValue( auditLogs, Document.parse( JsonUtils.toJson( document.get( "auditLogs" ) ) ) );
					if (null != auditLogs) {
						auditLogs.setStatus( status );
						Document auditLogsDoc = MongoDBUtils.transform2Doc( auditLogs );
						document.put( "auditLogs", auditLogsDoc );
					}

					DhResourceauditlogs dhResourceauditlogs = new DhResourceauditlogs();
					dhResourceauditlogs = (DhResourceauditlogs) MongoDBUtils.setFieldValue( dhResourceauditlogs,
							Document.parse( JsonUtils.toJson( document.get( "resourceauditlogs" ) ) ) );
					if (null != dhResourceauditlogs) {
						dhResourceauditlogs.setStatus( status );
						Document resourceauditlogsDoc = MongoDBUtils.transform2Doc( dhResourceauditlogs );
						document.put( "resourceauditlogs", resourceauditlogsDoc );
					}

					DhTodayStorageInfos dhTodayStorageInfos = new DhTodayStorageInfos();
					dhTodayStorageInfos = (DhTodayStorageInfos) MongoDBUtils.setFieldValue( dhTodayStorageInfos,
							Document.parse( JsonUtils.toJson( document.get( "todayStorageInfos" ) ) ) );
					if (null != dhTodayStorageInfos) {
						dhTodayStorageInfos.setStatus( status );
						Document todayStorageInfosDoc = MongoDBUtils.transform2Doc( dhTodayStorageInfos );
						document.put( "todayStorageInfos", todayStorageInfosDoc );
					}

					Bson resbatchAudit = new Document( "$set", document );
					manager.updateOne( filter, resbatchAudit, connName );
				}
			}
		}
	}
}